package com.atguigu.practice;

public class TheMostNumber {
    public static void main(String[] args) {
        int[] a=new int[]{1,2,3,2,2,2,5,4,2};
        System.out.println(MoreThanHalfNum(a));

    }
    public static int MoreThanHalfNum(int[] array){
        if(array==null||array.length==0){
            return 0;
        }
        int result=array[0];
        int times=1; //次数
        for(int i=1;i<array.length;i++){
            if(times!=0){
                if(array[i]==result){
                    times++; //相同则加一
                }else{
                    times--; //不同则减一
                }
            }else{
                result=array[i];
                times=1;
            }
        }
        times=0;
        for(int i=0;i<array.length;i++){
            if(array[i]==result){
                times++;
            }
        }
        return (times>array.length/2)?result:0;
    }
}
